package com.lindroid.materialdesigndemo.tablayoutdemo;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lindroid.materialdesigndemo.R;
import com.lindroid.materialdesigndemo.common.CommonAdapter;
import com.lindroid.materialdesigndemo.common.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author linyulong
 * @function 模拟评论界面
 */
public class CommentFragment extends android.support.v4.app.Fragment {
    private Context context;


    @Bind(R.id.rv_comment)
    RecyclerView rvComment;
    @Bind(R.id.swipe)
    SwipeRefreshLayout swipeRefreshLayout;

    private List<String> commentPerson = new ArrayList<>();
    private List<Integer> pictureIds = new ArrayList<>();

    public CommentFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_comment, container, false);
        ButterKnife.bind(this, view);
        context = getActivity();
        initData();
        initRefresh();
        initRecyclerView();
        final CommonAdapter<Integer> pictureAdapter = new CommonAdapter<Integer>(context, R.layout.item_picture, pictureIds) {
            @Override
            protected void convert(ViewHolder holder, Integer integer, int position) {
                holder.setImageResource(R.id.iv_comment_photo, integer);
            }
        };

        CommonAdapter<String> commentAdapter = new CommonAdapter<String>(context, R.layout.item_comment, commentPerson) {
            @Override
            protected void convert(ViewHolder holder, String s, int position) {
                holder.setText(R.id.tv_name, s);
                RecyclerView rvPicture = holder.getView(R.id.rv_picture);
                StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL) {
                    @Override
                    public boolean canScrollVertically() {
                        return false;
                    }

                    @Override
                    public boolean canScrollHorizontally() {
                        return false;
                    }
                };
                rvPicture.setLayoutManager(staggeredGridLayoutManager);
                rvPicture.setNestedScrollingEnabled(false);
                rvPicture.setAdapter(pictureAdapter);
            }
        };
        rvComment.setAdapter(commentAdapter);
        return view;
    }

    private void initData() {
        if (commentPerson.size() > 0) {
            commentPerson.clear();
        }
        if (pictureIds.size() > 0) {
            pictureIds.clear();
        }
        for (int i = 0; i < 10; i++) {
            commentPerson.add(new StringBuilder("评论").append(i).toString());
        }

        pictureIds.add(R.drawable.apple);
//        pictureIds.add(R.drawable.banana);
//        pictureIds.add(R.drawable.cherry);
//        pictureIds.add(R.drawable.grape);
//        pictureIds.add(R.drawable.orange);
//        pictureIds.add(R.drawable.pear);
    }

    private void initRecyclerView() {
        rvComment.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        rvComment.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
    }

    private void initRefresh() {
        //设置小圆圈的颜色，可以设置多个颜色来回切换
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light);
        //设置背景颜色
        swipeRefreshLayout.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(context, android.R.color.white));
        //设置滑动距离
        swipeRefreshLayout.setDistanceToTriggerSync(100);
        //设置大小模式
        swipeRefreshLayout.setSize(SwipeRefreshLayout.DEFAULT);
        //设置下拉刷新的监听
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler(new Handler.Callback() {
                    @Override
                    public boolean handleMessage(Message msg) {
                        swipeRefreshLayout.setRefreshing(false);
                        return false;
                    }
                }).sendEmptyMessageDelayed(0, 2000);
            }
        });
        swipeRefreshLayout.refreshDrawableState();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
